import ConcreteBuilder.*;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import java.io.File;
public class Main {
    //方法一：直接写入的方式进行显示
//    public static void main(String[] args) {
//        AnimationDirector director1=new AnimationDirector(new AnimationBuilderKyoto());
//        ProductAnimaiton animaiton1=director1.constructAnimation();
//        System.out.println(animaiton1.toString());
//
//        AnimationDirector director2=new AnimationDirector(new AnimationBuilderUfotable());
//        ProductAnimaiton animaiton2=director2.constructAnimation();
//        System.out.println(animaiton2.toString());
//
//        AnimationDirector director3=new AnimationDirector(new AnimationBuilderAniplex());
//        ProductAnimaiton animaiton3=director3.constructAnimation();
//        System.out.println(animaiton3.toString());
//    }

    //方法二：采用xml的方式进行循环读入
    public static void main(String[] args) {
        try {
            // 读取XML中的动画构建器类名
            for (int i = 1; i <= readClassCountFromXML(); i++) {
                String className = readClassNameFromXML("builder" + i);
                if (!className.isEmpty()) {
                    // 通过反射实例化类
                    Class<?> classReflection = Class.forName(className);
                    Object objectReflection = classReflection.getDeclaredConstructor().newInstance();

                    AnimationBuilder builder = (AnimationBuilder) objectReflection;

                    // 使用 AnimationDirector 构建动画
                    AnimationDirector director = new AnimationDirector(builder);
                    ProductAnimaiton animation = director.constructAnimation();
                    System.out.println(animation.toString());
                } else {
                    System.out.println("未找到id编号为builder" + i + "的动画构建器");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static String readClassNameFromXML(String id) {
        String className = "";
        try {
            // 读取XML文件
            DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = dFactory.newDocumentBuilder();
            Document doc = builder.parse(new File("class_7//src//config.xml"));

            // 获取指定ID的类名节点
            NodeList nl = doc.getElementsByTagName("animation");
            for (int i = 0; i < nl.getLength(); i++) {
                Element element = (Element) nl.item(i);
                String animationId = element.getAttribute("id");
                if (animationId.equals(id)) {
                    className = element.getElementsByTagName("className").item(0).getTextContent();
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return className;
    }

    // 返回XML里有多少个动画构建器
    public static int readClassCountFromXML() {
        int classCount = 0;
        try {
            // 读取XML文件
            DocumentBuilderFactory dFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = dFactory.newDocumentBuilder();
            Document doc = builder.parse(new File("class_7//src//config.xml"));

            // 获取动画构建器节点
            NodeList nl = doc.getElementsByTagName("animation");
            classCount = nl.getLength();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return classCount;
    }

}